package com.jy.appmarket.screenlock.view;

import java.lang.reflect.Field;

import android.content.Context;
import android.graphics.PointF;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.Interpolator;
import android.widget.Scroller;

public class SmoothViewPager extends ViewPager {

	private boolean mEnableSliding = true;
	private PointF mDownP = new PointF();
	private PointF mCurP = new PointF();
	OnSingleTouchListener onSingleTouchListener;
	private OnTouchEventBKListener mEventBKListener;
	private boolean mNoPageScroll = false;

	public SmoothViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		try {
			Field mScroller = ViewPager.class.getDeclaredField("mScroller");
			mScroller.setAccessible(true);
			FixedSpeedScroller scroll = new FixedSpeedScroller(context);
			mScroller.set(this, scroll);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public interface OnTouchEventBKListener{
		void onTouchEvent_BK(MotionEvent event);
	}

	public class FixedSpeedScroller extends Scroller {

		private int mDuration = 1000;

		public FixedSpeedScroller(Context context) {
			super(context);
		}

		public FixedSpeedScroller(Context context, Interpolator interpolator) {
			super(context, interpolator);
		}

		@Override
		public void startScroll(int startX, int startY, int dx, int dy, int duration) {
			super.startScroll(startX, startY, dx, dy, mDuration);
		}

		@Override
		public void startScroll(int startX, int startY, int dx, int dy) {
			super.startScroll(startX, startY, dx, dy, mDuration);
		}
	}

	/**
	 * 是否启用手势滑动，默认启用
	 * 
	 * @param enable
	 */
	public void enableSliding(boolean enable) {
		mEnableSliding = enable;
	}
	
	/**
	 * 是否禁止左右滑动, 默认不禁止
	 * @param enable
	 */
	public void setNoPageScroll(boolean noScroll){
		mNoPageScroll = noScroll;
	}
	
	/**
	 * 设置获取手势操作的事件监听
	 * @param l
	 */
	public void setOnTouchEventBKListener(OnTouchEventBKListener l){
		mEventBKListener = l;
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent arg0) {
		if (mEnableSliding) {
			return true;
		}
		if (mNoPageScroll) {
			return false;
		}
		return super.onInterceptTouchEvent(arg0);
	}

	@Override
	public boolean onTouchEvent(MotionEvent arg0) {
		if(mEventBKListener != null){
			mEventBKListener.onTouchEvent_BK(arg0);
		}
		if (mEnableSliding) {
			mCurP.x = arg0.getX();
			mCurP.y = arg0.getY();

			if (arg0.getAction() == MotionEvent.ACTION_DOWN) {
				mDownP.x = arg0.getX();
				mDownP.y = arg0.getY();
				 getParent().requestDisallowInterceptTouchEvent(true);
			}

			if (arg0.getAction() == MotionEvent.ACTION_MOVE) {
				if ((int)Math.abs(mCurP.y - mDownP.y) > 50 && (int) Math.abs(mCurP.x - mDownP.x) < 50) {
					getParent().requestDisallowInterceptTouchEvent(false);
				} else{
					getParent().requestDisallowInterceptTouchEvent(true);
				}
			}

			if (arg0.getAction() == MotionEvent.ACTION_UP) {
				if ((int) Math.abs(mCurP.x - mDownP.x) <= 10 && (int) Math.abs(mCurP.y - mDownP.y) <= 10) {
					onSingleTouch();
					return true;
				}
			}
		}
		if (mNoPageScroll) {
			return false;
		}
		
		return super.onTouchEvent(arg0);
	}

	public void onSingleTouch() {
		if (onSingleTouchListener != null) {
			onSingleTouchListener.onSingleTouch();
		}
	}

	public interface OnSingleTouchListener {
		void onSingleTouch();
	}

	public void setOnSingleTouchListener(OnSingleTouchListener onSingleTouchListener) {
		this.onSingleTouchListener = onSingleTouchListener;
	}

}
